package com.icinfo.cloud.provider.common.enums;

import com.icinfo.cloud.provider.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.ObjectUtils;

/**
 * 描述：接口访问数据来源类型枚举
 *
 * @author xiangyuyu
 * @date 2022/8/10
 * @since 1.3.2
 */
@AllArgsConstructor
public enum ApiVisitDataSourceEnum {
    /**
     * 接口访问数据来源类型枚举
     */
    PUNISH_PC("ec158714776d4ff69a8a53d2096cd034", "PUNISH_PC", "统一处罚pc端"),
    PUNISH_MOBILE("7351abc41f1244809f3ae5eb563bb15f", "PUNISH_MOBILE", "统一处罚掌上端"),
    ZJSXFJDZFXT_MOBILE("fed06ec7054b4d1ba125df1271d20ae4", "ZJSXFJDZFXT_MOBILE", "浙江省消防监督执法系统掌上端"),
    PUNISH_AY("93009a886cad217bfada654e0dcff67a", "PUNISH_AY", "统一处罚pc端案源"),
    TASKCENTER("TASKCENTER", "TASKCENTER", "大综合一体化平台(检查)"),
    TASKCENTER_TRANSFER("", "TASKCENTER_TRANSFER", "大综合一体化平台(协同)"),
    KJZF("KJZF", "KJZF", "快捷执法");

    /**
     * id
     */
    @Getter
    private String id;
    /**
     * 编码
     */
    @Getter
    private String code;
    /**
     * 描述
     */
    @Getter
    private String des;


    /**
     * 描述：根据id获取des
     *
     * @param id 编码
     * @return 描述内容
     * @author xiangyuyu
     * @date 2022-02-22 10:43
     */
    public static String getDesById(String id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        for (ApiVisitDataSourceEnum e : ApiVisitDataSourceEnum.values()) {
            if (id.equals(e.getId())) {
                return e.getDes();
            }
        }
        return id;
    }


    /**
     * 描述：根据code获取des
     *
     * @param code 编码
     * @return 描述内容
     * @author xiangyuyu
     * @date 2022-02-22 10:43
     */
    public static String getDesByCode(String code) {
        if (ObjectUtils.isEmpty(code)) {
            return null;
        }
        for (ApiVisitDataSourceEnum e : ApiVisitDataSourceEnum.values()) {
            if (code.equals(e.getCode())) {
                return e.getDes();
            }
        }
        return code;
    }

    /**
     * 描述：校验id是否是合法枚举
     *
     * @param id 枚举id
     * @return boolean 是否合法枚举（true-是/false-否）
     * @author xiangyuyu
     * @date 2022-02-22 10:42
     */
    public static boolean isLegalEnumId(String id) {
        boolean flag = false;
        for (ApiVisitDataSourceEnum e : ApiVisitDataSourceEnum.values()) {
            if (e.getId().equalsIgnoreCase(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }


    /**
     * 描述：判断数据来源id是否为外部系统
     *
     * @param id 数据来源id
     * @return 是否为外部系统(0 - 否 / 1 - 是)
     * @author xiangyuyu
     * @date 2022/8/12
     * @since 是否为外部系统
     */
    public static boolean isOutSystem(String id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        if (ApiVisitDataSourceEnum.PUNISH_PC.getId().equals(id) ||
                ApiVisitDataSourceEnum.PUNISH_MOBILE.getId().equals(id)
        ) {
            return false;
        } else if (ApiVisitDataSourceEnum.isLegalEnumId(id)) {
            return true;
        }
        throw new BusinessException("请指定有效的数据来源");
    }

}
